package com.google.coffeeshop.gui.desktop.seat;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.business.MaintenanceBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.AbstractBasePanel;
import com.google.coffeeshop.gui.desktop.employee.EmployeeActionsPanel;
import com.google.coffeeshop.gui.desktop.orderpayment.OrderPaymentPanel;

public class SeatActionPanel  extends AbstractBasePanel{
	
	private SeatPanel seatPanel;
	private OrderPaymentPanel pnlOrderPayment;
	
	public SeatActionPanel(final SeatPanel panel, OrderPaymentPanel pnlOrderPayment) {
		this.pnlOrderPayment = pnlOrderPayment;
		this.seatPanel = panel;
		JToolBar toolbar = new JToolBar();
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AddEditSeatFrame frmProduct = new AddEditSeatFrame(seatPanel);
				frmProduct.setVisible(Boolean.TRUE);
			}
		});
		btnAdd.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnAdd.setHorizontalTextPosition(SwingConstants.CENTER);
		btnAdd.setIcon(new ImageIcon(EmployeeActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/add-24.png")));
		setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		toolbar.add(btnAdd);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				long productID = seatPanel.getProductSelectedId();
				if(productID >0){
					AddEditSeatFrame frmProduct = new AddEditSeatFrame(seatPanel, productID);
					frmProduct.setVisible(Boolean.TRUE);
				}
			}
		});
		btnEdit.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnEdit.setHorizontalTextPosition(SwingConstants.CENTER);
		btnEdit.setIcon(new ImageIcon(EmployeeActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/edit-24.png")));
		
		toolbar.add(btnEdit);

		add(toolbar);
		
		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startSeats();
				refreshParentGUI();
			}
		});
		btnStart.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnStart.setHorizontalTextPosition(SwingConstants.CENTER);
		btnStart.setIcon(new ImageIcon(SeatActionPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/start-24.png")));
		toolbar.add(btnStart);
	}

	private void startSeats() {
		if (seatPanel != null && !seatPanel.getSeatsSelected().isEmpty()) {
			for (SeatDTO seat : seatPanel.getSeatsSelected()) {
				if (seat.getStatus().equals(GeneralConstants.SEAT_NEW_STATUS)) {
					MaintenanceBusiness.startSeat(seat.getId());
				}
			}
			seatPanel.refreshGUI();
		}
	}
	
	private void refreshParentGUI() {
		pnlOrderPayment.refreshGUI();
	}
	
	private static final long serialVersionUID = 1L;
	
}
